﻿using System;
using UnityEngine;

namespace ZCUIManager
{
    public abstract class UIBaseView : UIBaseGraphic
    {
        #region ProtectedField
        [SerializeField] protected int m_BigLayer = 0;

        [SerializeField] protected int m_LayerWeight;

        protected eUIType m_UIType;

        protected GameObject m_GameObject;

        protected RectTransform m_RectTransform;
        #endregion

        #region PublicMethod
        public int BigLayer => m_BigLayer;

        public int LayerWeight
        {
            get
            {
                return m_LayerWeight;
            }
            set
            {
                if (m_LayerWeight == value)
                {
                    return;
                }
                m_LayerWeight = value;
                UIManager.Instance.UpdateLayer();
            }
        }

        public eUIType UIType => m_UIType;

        public virtual bool isUIView => true;

        public virtual bool isSingleton => false;
        #endregion

        #region  StaticMethod
        protected static GameObject Find(RectTransform transform, string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return null;
            }
            if (name.Equals(transform.name))
            {
                return transform.gameObject;
            }
            GameObject childGo = null;
            int childCount = transform.childCount;
            for (int i = 0; i < childCount; i++)
            {
                childGo = Find(transform.GetChild(i).GetComponent<RectTransform>(), name);
                if (childGo != null)
                {
                    return childGo;
                }
            }
            return childGo;
        }
        #endregion

        #region VirtualMethod
        public virtual void Initialize(eUIType uiType, GameObject go)
        {
            m_UIType = uiType;
            m_GameObject = go;
            m_RectTransform = m_GameObject.GetComponent<RectTransform>();
            // m_RectTransform.anchoredPosition = Vector2.zero;
        }

        public virtual void InitializeListener()
        {

        }

        public virtual void SetParent(RectTransform parent)
        {
            m_RectTransform.SetParent(parent, false);
        }

        public virtual void Show()
        {
            m_GameObject.SetActive(true);
        }

        public virtual void Tick()
        {

        }

        public virtual void Hide()
        {
            m_GameObject.SetActive(false);
        }

        public virtual void Uninitialize()
        {
            GameObject.Destroy(m_GameObject);
            m_GameObject = null;
            m_RectTransform = null;
        }
        #endregion

        #region Public Method
        public void SetSiblingIndex(int index)
        {
            m_RectTransform.SetSiblingIndex(index);
        }
        #endregion
    }
}
